package com.ruoyi.boat.controller;

import com.ruoyi.boat.aop.AvoidDuplicate;
import com.ruoyi.boat.domain.Status;
import com.ruoyi.boat.service.IBoatService;
import com.ruoyi.boat.service.IStatusService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 状态Controller
 *
 * @author ruoyi
 * @date 2023-07-31
 */
@Controller
@Slf4j
@RequestMapping("/boat/status")
public class StatusController extends BaseController {
    private String prefix = "boat/status";

    @Autowired
    private IStatusService statusService;

    @Autowired
    private IBoatService boatService;

    @RequiresPermissions("boat:status:view")
    @GetMapping()
    public String status() {
        return prefix + "/status";
    }

    /**
     * 查询状态列表
     */
    @RequiresPermissions("boat:status:list")
    @PostMapping("/list")
    @ResponseBody
    @ApiOperation("状态列表")
    public TableDataInfo list(Status status) {
        startPage();
        status.setTime(new Date());
        List<Status> list = statusService.selectStatusList(status);
        return getDataTable(list);
    }

    /**
     * 导出状态列表
     */
    @RequiresPermissions("boat:status:export")
    @Log(title = "状态", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @AvoidDuplicate
    @ResponseBody
    public AjaxResult export(Status status) {
        List<Status> list = statusService.selectStatusList(status);
        ExcelUtil<Status> util = new ExcelUtil<>(Status.class);
        return util.exportExcel(list, "状态数据");
    }

    /**
     * 新增状态
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存状态
     */
    @RequiresPermissions("boat:status:add")
    @Log(title = "状态", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Status status) {
        return toAjax(statusService.insertStatus(status));
    }

    /**
     * 修改状态
     */
    @RequiresPermissions("boat:status:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Status status = statusService.selectStatusById(id);
        mmap.put("status", status);
        return prefix + "/edit";
    }

    /**
     * 修改保存状态
     */
    @RequiresPermissions("boat:status:edit")
    @Log(title = "状态", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Status status) {
        return toAjax(statusService.updateStatus(status));
    }

    /**
     * 删除状态
     */
    @RequiresPermissions("boat:status:remove")
    @Log(title = "状态", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(statusService.deleteStatusByIds(ids));
    }
}
